package club.itstudio.itouc.service.schedule

import club.itstudio.itouc.service.JwOps
import club.itstudio.itouc.util.Redis
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service

/**
 * 定时删除过期事件，分布式锁
 */
@Service
class ScheduleDelete (val redis: Redis,
                      val updateCourseService: UpdateCourseService,
                      val jwOps: JwOps){

    // 定时删除分布式锁，防止死锁
    @Scheduled(cron = "0 0/10 * * * ?")
    fun deleteLock() {
        val lockMap = redis.getAllLock()
        val now = System.currentTimeMillis()
        lockMap.forEach { (task, expire) ->
            if (now > expire) {
                redis.deleteLock(task)
            }
        }
    }
}